const userList = [
  {
    tel: "13111111111",
    avator: "",
    nickname: "阿三",
  },
  {
    tel: "14444444444",
    avator: "",
    nickname: "李四",
  },
]
const bodyParser = require("body-parser")
const multipart = require("connect-multiparty")
const multipartMiddleware = multipart()
const OSS = require("ali-oss")
const path = require("path")
const AlipaySdk = require("alipay-sdk/lib/form").default
const client = new OSS({
  region: "oss-cn-shanghai",
  accessKeyId: "LTAI5tSNQ6Fzy3LMkUcZjVL9",
  accessKeySecret: "YGpxnxvbm4BD0VxUjmTl49S0bXLHSA",
  bucket: "2007a-1",
})
const alipaySdk = new AlipaySdk({
  appId: "2021000119601763",
  privateKey: `MIIEowIBAAKCAQEAw8n+AicnWKCsuW0IaBaWBBU07cMuBLvDPG9AXjSOwIcEH9udgbwvLhYL2FNVL5icv9Dd/VxDBfh6ruNqovhatq1lX8psa32+MiuJbfVqysoxnuGboV9aXfl8XDcrrzW42JVkTzfy73PC6iOeccYH9YFzL8jJdQN4Iov9swAGVA7n87TkI/I+NhI/hz3bC7AXYjF9Ozi+/pXRel/YsptfmmHteSuQ9R1N7q1pQceuF5fSPh3eVEKn94OvO1fzmQdR0uBu3c0RFhWD1WaINsKSsSP0vihDUwNJK74XoTrbZVnvOhdpKTCH8sNqM8ST+M3UZ+WO/Ia1Q1imS1dMG6jM+QIDAQABAoIBAHeR7Kaz8ruVpOa5R+EJqiRkLyblhD0vzlQiPVVLE8NQsZwjK1YH72hd3m4GtoeS1GfwzyHkLqp+za3xBTbyJbRawQZ3QS+EY5uQKyYUM1wVj6hQcBNlgHLT4rcOmocub+0Vj2Ux7hN+xxLnHeSt5D5zCD2uihoEPGIczJt88GB0hi+JqZitGWs+X0voQouPusAfW4F/qN+mgcnxGrnQ/5raWTFOlLPw/uxNdsZTdQ4fjfGTpOkK859uDN9rjxcvacWMRS4QFqg7UZuS1DQ0cKm6jG4PPBZqPf+4fyLQjZYyssbdQ5lxrO9bO8Bw1Pqza+nfUkN5S5Xf2B5MM4D1Y4ECgYEA/2QpVl31YFaKqDVuF6pdExGK8QIuqbOZmSBa5g0+YY9v0QEGJl61HzHfooHVGlrXa/V6+dL1MLFDD6SvWyVjfSIXSJ4shNQ2L7yBdmd/ZTTYMLNEfaIedJCvAgxI2UfwstsDO3VDz90ZSK7h72JGLjjnIhUznu6l5NnPqjHF0A0CgYEAxEF2NX3L1UZHaJZhmwjJbvPrF2XX7A4L634TPtF/n/gNQZhILExhNIlz9F5aevYVNlSzvKpXXOgxS9cuKCsxnWVf7Ym83ZAzLud88QQq11QBU4RxzfG8bf+q2diX2Gt+XM2AfPjrF49YFi/qO4PMolz5b37QaCfnN43ZmBt2yZ0CgYEAyeME5nhKA8kFvSjXia+r+enxOsnt10rZjfnMwXUQrhC0ZLT5hpVxemJzlcQqkLXk+zvH5ftExiQqjhAyasvolL/BRi97o9kZqKzMbqwsQfX/PKhJ6UKh7qV9IxJQ+CptdQW6zWLKo2P1m8ZbZl/6eImU9RVrhCTFUV55UeIcrHUCgYAmJRsWtoahw3EpsODzwzdzq1EsUQ0wwmIquWyALzQBIbNh5nlQst/J2FWDT7uCXsYsszXkG6+zTSYXuZEtGYghMYz9nGHrJpXsLe4o4kh5ZciENR/xiWS4dJL+mIya8fmzi/XHVJJNF/1lApciTjG99Rg+RYW4WNkmARoj6GKkOQKBgHWgFL+qxVZra8m13QA0iKhHcWEYmqG+GnhNzBu/EbEnX4Zc1wxI/uC+o7K+Q4L8K4kVJJyHAxCZsPPS1UhPzCAR0lyCCpl/xmmsPcXDWnxnPkZMlujwAHDKmwnK1nAu0lwC6XBJhJYQ/WDRz0apM38QMNL5SmdYfIgBEnooo2np`,
  gateway: "https://openapi.alipaydev.com/gateway.do",
})
module.exports = (app) => {
  app.use(bodyParser.json())
  app.get("/api/tel", (req, res) => {
    const { tel } = req.query
    const user = userList.find((v) => v.tel === tel)
    if (user) {
      globalThis.code = Math.random().toString().slice(2, 6)
      res.send({
        code: 200,
        data: {
          code: globalThis.code,
        },
      })
    } else {
      res.send({
        code: 404,
        message: "该手机号未注册",
      })
    }
  })
  app.post("/api/tel/login", (req, res) => {
    const { tel, code } = req.body
    const user = userList.find((v) => v.tel === tel)
    if (code === globalThis.code && user) {
      res.send({
        code: 200,
        data: {
          avator: user.avator,
          tel: user.tel,
          nickname: user.nickname,
        },
        message: "登录成功",
      })
    } else {
      res.send({
        code: 403,
        data: null,
        message: "手机号或者验证码错误",
      })
    }
  })
  app.post("/api/upload", multipartMiddleware, async (req, res) => {
    const { path, name } = req.files.chunk
    const result = await client.put(name, path)
    res.send({
      code: 200,
      data: result,
    })
  })
  app.post("/api/pay", async (req, res) => {
    const { id, price, title } = req.body
    const formData = new AlipayFormData()
    formData.setMethod("get")
    formData.addField("return_url", "http://localhost:3000/order/query")
    formData.addField("bizContent", {
      out_trade_no: id,
      total_amount: price,
      subject: title,
    })
    const result = await alipaySdk.exec(
      "alipay.trade.wap.pay",
      {},
      { formData: formData }
    )
    console.log(result)
    res.send({
      code: 200,
      data: result,
    })
  })
}
